<template>
  <div class="login-container">
    <div class="login-box">
      <div class="logo">
        <h1>大麦电影票务平台</h1>
      </div>
      <el-form :model="form" :rules="rules" ref="loginForm">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户名/手机号" prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="密码" prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="submitForm" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="footer-links">
        <!-- 修复这里：使用 router-link 代替原生 a 标签 -->
        <router-link to="/register">注册新账号</router-link>
        <a href="#">忘记密码</a>
      </div>
      <div class="copyright">
        © 2023 大麦电影票务平台 版权所有
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '@/store/auth';
import authService from '@/services/auth';

const form = ref({ username: '', password: '' });
const rules = ref({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
});
const loginForm = ref(null);
const loading = ref(false);
const router = useRouter();
const authStore = useAuthStore();

const submitForm = () => {
  loginForm.value.validate(valid => {
    if (valid) {
      loading.value = true;
      authService.login(form.value.username, form.value.password)
          .then(response => {
            if (response.code === 200) {
              authStore.setToken(response.data.token);
              authStore.setUser(response.data.user);
              router.push('/');
            } else {
              throw new Error(response.message || '登录失败');
            }
          })
          .catch(error => {
            console.error('登录错误', error);
          })
          .finally(() => {
            loading.value = false;
          });
    }
  });
};
</script>
<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-box {
  width: 380px;
  padding: 40px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.logo {
  margin-bottom: 30px;
}

.logo h1 {
  color: #e54847;
  font-size: 28px;
  letter-spacing: 2px;
}

.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  margin-top: 10px;
  background-color: #e54847;
  border: none;
}

.login-btn:hover {
  background-color: #d03c3b;
}

.footer-links {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  font-size: 14px;
}

.footer-links a {
  color: #008cd6;
  text-decoration: none;
}

.footer-links a:hover {
  text-decoration: underline;
}

.copyright {
  margin-top: 30px;
  color: #666;
  font-size: 12px;
}
</style>